package com.pg_chat.controller;

import com.pg_chat.config.YmlProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController("configsController")
@RequestMapping(value = "/configs", method = RequestMethod.GET)
public class ConfigsController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private YmlProperties properties;

    private Long time = 0L;

    @RequestMapping("/author")
    public String getAuthor(HttpServletRequest request){
        JSONObject authorData = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            authorData = (JSONObject) JSONObject.toJSON(properties);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",0);
            jsonObject.put("message","失败");
            jsonObject.put("data",0);
        }

        jsonObject.put("code",1);
        jsonObject.put("message","成功");
        jsonObject.put("data",authorData);
        System.out.println(request.getRequestURI());
        //System.out.println(request.getHeader("Referer"));
        System.out.println(request.getScheme()+"://" + request.getServerName() + request.getContextPath());
        StringBuffer url = request.getRequestURL();
        System.out.println(url.replace(url.lastIndexOf("/"),url.length(),"/wxLogin"));
        return jsonObject.toJSONString();
    }

    @RequestMapping(value = "/httpConnTest",method = RequestMethod.POST)
    public String connTest(@RequestParam("code") String code, HttpServletRequest request){
        System.out.println("find code "+code);
        JSONObject json = new JSONObject();
        String message = "auth error";
        Integer status = 404;
        //System.out.println(connStatus);
        if("505".equals(code)){
            status = 200;
            message = "ok";
            time++;
            System.out.println("find code!"+time);
        }
        json.put("message", message);
        json.put("status", status);

        return json.toJSONString();
    }

}
